<template>
  <el-row class="bg-cyan-500 min-h-screen">
    <el-col :lg="16" :md="12" class="custom-flex-hear">
      <div>
        <div class="font-bold text-5xl text-gray-50 mb-4">欢迎光临</div>
        <div class="text-sm text-gray-200">测试内容咿咿呀呀</div>
      </div>
    </el-col>
    <el-col :lg="8" :md="12" class="bg-white custom-flex-style">
      <h2 class="font-bold text-3xl text-gray-800 mb-3">登录</h2>
      <div class="main-password">
        <span class="h-[1px] w-16 bg-gray-200"></span>
        <span>账号密码登录</span>
        <span class="h-[1px] w-16 bg-gray-200"></span>
      </div>
      <el-form
        ref="formRef"
        :model="from"
        class="w-[250px]"
        :rules="rules"
        @keyup.enter="onSubmit"
      >
        <el-form-item prop="username">
          <el-input v-model="from.username" placeholder="请输入账号">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="from.password"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="onSubmit"
            type="primary"
            round
            color="#22d3ee"
            class="w-[250px]"
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
        <!-- <el-form-item>
          <el-button color="#22d3ee" round class="w-[250px]" type="primary"
            >注册</el-button
          >
        </el-form-item> -->
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { showMessage } from "@/composables/utils";
import { useStore } from "vuex";

const formRef = ref(null);
const router = useRouter();
const loading = ref(false);

const store = useStore();

const from = reactive({
  username: "",
  password: "",
});

const onSubmit = () => {
  formRef.value.validate((vaild) => {
    if (!vaild) {
      return false;
    }
    // 开始加载
    loading.value = true;
    store
      .dispatch("login", from)
      .then((res) => {
        //console.log(res);
        showMessage("登录成功");
        router.push("/");
      })
      .finally(() => {
        // 结束加载
        loading.value = false;
      });
  });
};

const rules = {
  username: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 5, max: 20, message: "账号长度在5到20个字符", trigger: "blur" },
  ],

  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    // { min: 6, max: 20, message: "密码长度在6到20个字符", trigger: "blur" },
  ],
};
</script>

<style>
.custom-flex-style {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.custom-flex-hear {
  width: 100%;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}

.min-h-screen .main-password {
  @apply flex items-center justify-center mb-3 text-gray-300 space-x-2;
}
</style>